package com.lill.system.serivce.impl;

import java.beans.PropertyDescriptor;
import java.util.List;

import javax.annotation.Resource;

import org.apache.shiro.SecurityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import com.lill.system.constant.SystemConstant;
import com.lill.system.service.IShiroAuthService;

@Service("shiroAuthService")
public class ShiroAuthServiceImpl implements IShiroAuthService {
	
	protected Logger logger = LoggerFactory.getLogger(getClass());
	
    @Resource(name="commonRedisTemplate")
    private RedisTemplate<String, List<String>> redisTemplate;

	public boolean hasPermission(String perm) {
		boolean result=false;
		
		Object userObj=SecurityUtils.getSubject().getPrincipal();
		String userId=null;
		try {
			userId = new PropertyDescriptor("id", userObj.getClass()).getReadMethod().invoke(userObj).toString();
		} catch (Exception e) {
			logger.error(e.getMessage(), e);
		}
		List<String> permlist=redisTemplate.opsForValue().get(SystemConstant.SYS_PERMS_KEY+userId);
		if(null!=permlist&&!permlist.isEmpty()){
//			logger.info("*************button--start------------------------");
//			String permVals="";
//			for (String permVal : permlist) {
//				permVals+=permVal+"**";
//			}
//			logger.info(permVals);    别打开了 ,那怎么知道按钮是否有权限？？？
//			logger.info("*************button--end------------------------");
//			logger.info("perm:"+perm);
			result=permlist.contains(perm);
		}
		return result;
	}

}
